package com.dqp.cslggroup.UI;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.dqp.cslggroup.R;
import com.dqp.cslggroup.Util.CompressedBitmap;
import com.dqp.cslggroup.Util.GetTimeUtil;
import com.dqp.cslggroup.bean.event;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class EventAdapter extends BaseAdapter {
    private List<event> events;
    private LayoutInflater inflater;


    public EventAdapter(List<event> events, Context context) {
        this.events = events;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return events == null ? 0 : events.size();
    }

    @Override
    public Object getItem(int position) {
        return events.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        @SuppressLint("ViewHolder") View view = inflater.inflate(R.layout.item_event, null);
        event event = (event) getItem(position);
        TextView content = view.findViewById(R.id.tv_content);
        TextView count = view.findViewById(R.id.tv_count);
        TextView date = view.findViewById(R.id.tv_date);
        TextView msg = view.findViewById(R.id.tv_msg);
        ImageView img = view.findViewById(R.id.iv_pic);
        ImageView big_bg = view.findViewById(R.id.iv_pic_bg);
        TextView zd = view.findViewById(R.id.tv_zd);
        int select = event.getSelect();
        Date d = new Date();
        @SuppressLint("SimpleDateFormat") DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String Date = df.format(d);
        String event_date = event.getDate();
        if (select == 0) {
            content.setText(event.getName());
            count.setText("第" + GetTimeUtil.addDates(event_date, Date) + "天");

        } else if (select == 1) {
            int counts = GetTimeUtil.addDates(Date, event_date);

            if (counts > 0) {
                content.setText("距离" + event.getName() + "的生日还有");
                count.setText(counts + "天");
            } else {
                content.setText(event.getName() + "的生日已过去");
                count.setText(GetTimeUtil.addDates(event_date, Date) + "天");
            }

        } else {
            int counts = GetTimeUtil.addDates(Date, event_date);
            if (counts > 0) {
                content.setText(event.getName() + "仅剩");
                count.setText(counts + "天");
            } else {
                content.setText(event.getName() + "已过去");
                count.setText(GetTimeUtil.addDates(event_date, Date) + "天");
            }
        }
        date.setText(event.getDate());
        msg.setText(event.getBz());
        if (event.getBg() != null) {
            if (CompressedBitmap.fileExist(event.getBg())) {
                Bitmap bitmap = BitmapFactory.decodeFile(event.getBg());
                if (bitmap != null) {
                    bitmap = CompressedBitmap.Compressed(bitmap, 0.1f);
                    img.setImageBitmap(bitmap);
                    big_bg.setImageBitmap(bitmap);
                }
            }
        }
        if (event.getZd() == 1)
            zd.setVisibility(View.VISIBLE);
        return view;
    }


}
